<?php
include_once ("model/accountModel.php");
include_once ("_include/common.php");
class accountController {
	
	function __construct() {
		if (!$_REQUEST['pagesize']) {
			$_REQUEST['pagesize'] = 1;
		}
		if (!$_REQUEST['page']) {
			$_REQUEST['page'] = 1;
		}
		$this -> pagesize = $_REQUEST['pagesize'];
		$this -> currentpage = $_REQUEST['page'];
		$this -> model = new accountModel($this -> pagesize);
	}

	public function invoke() {
		if ($_REQUEST['Submit']) {

			switch ($_REQUEST['Submit']) {
				case 'Add' :
					$this -> addAccount();
					break;
				case 'Modify' :
					$this -> modifyAccount();
					break;
			}

		} else {
			if ($_REQUEST['do']) {
				switch ($_REQUEST['do']) {
					case 'select' :
						$this -> loadAccount();
						break;
					case 'delete' :
						$this -> deleteAccount();
						break;
				}
			} else {
				$this -> loadAccounts();
			}
		}
	}

	public function addAccount() {

		$Account = $_REQUEST['Account'];
		$Password = $_REQUEST['Password'];
		$Name = $_REQUEST['Name'];
		$Email = $_REQUEST['Email'];
		$Birthday = $_REQUEST['Birthday'];
		$Role = $_REQUEST['Role'];
		$Address = $_REQUEST['Address'];
		$Phone = $_REQUEST['Phone'];
		$About = $_REQUEST['About'];

		$MSS = $this -> model -> addAccount($Account, $Password, $Name, $Email, $Birthday, $Role, $Address, $Phone, $About);
		include 'view/admin/account.php';
	}

	public function modifyAccount() {
		$Account = $_REQUEST['Account'];
		$Password = $_REQUEST['Password'];
		$Name = $_REQUEST['Name'];
		$Email = $_REQUEST['Email'];
		$Birthday = $_REQUEST['Birthday'];
		$Role = $_REQUEST['Role'];
		$Address = $_REQUEST['Address'];
		$Phone = $_REQUEST['Phone'];
		$About = $_REQUEST['About'];

		$MSS = $this -> model -> modifyAccount($Account, $Password, $Name, $Email, $Birthday, $Role, $Address, $Phone, $About);
		include 'view/admin/account.php';
	}

	public function loadAccount() {
	$account = $_REQUEST["Account"];
		$model = new accountModel(1);
		$accs = $model -> selectAccount($account);		
		foreach ($accs as $acc) {
			echo json_encode($acc);
		}
	}

	public function loadAccounts() {
		$Accounts = $this -> model -> selectAccounts($this -> currentpage);
		$pagecount = $this -> model -> pagecount;
		$PageLink = getPageLinks($pagecount);

		include 'view/admin/account.php';
	}

	public function deleteAccount() {

	}

}
